package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ab.soap.AbSoapUtil;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.GcwxList;
import com.htandroid.htandroid.excel.ExcelMaintain_wxsh;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.BuildGcwxXMLService;
import com.htandroid.htandroid.webservice.XMLGcwxVouchlist;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;
import static com.htandroid.htandroid.excel.ExcelGetDir.getExcelDir;

/**
 * Created by Administrator on 2015/12/24.
 */
public class Gcwx_check_index extends base_index {
    private ArrayList<GcwxList> vouchlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_gcwx_check_index);
        initview("维修审核");
        initview2();
        getdata(0);
    }

    //初始化2
    @Override
    public void initview2() {
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String excelName = "0".equals(listtype) ? "未审核" : "已审核";
                excelPath = getExcelDir() + File.separator + "维修审核" + excelName + ".xls";
                ExcelMaintain_wxsh wxsh = new ExcelMaintain_wxsh(Gcwx_check_index.this, excelPath, vouchlist, listtype);
                if (vouchlist.size() == 0) {
                    AbToastUtil.showToast(getApplicationContext(), "列表中没有数据，不能分享");
                } else {
                    OnekeyShare oks = new OnekeyShare();
                    oks.setTitle("维修审核" + excelName + ".xls");
                    oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                        @Override
                        public void onShare(Platform platform,
                                            cn.sharesdk.framework.Platform.ShareParams paramsToShare) {
                            // TODO Auto-generated method stub
                            if (platform.getName().equals("Wechat")) {
                                paramsToShare.setShareType(Platform.SHARE_FILE);
                            }
                        }
                    });
                    imgPath = getExcelDir() + File.separator + "excel.png";
                    writeImg(imgPath);
                    oks.setImagePath(imgPath);
                    oks.setFilePath(excelPath);
                    oks.show(getApplicationContext());
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //处理选状态
                linechoose("r", Integer.valueOf("" + id));
                //从position改为id,因为过滤之后的position不正确
                Intent intent = new Intent(getApplicationContext(), Gcwx_check_detail.class);
                //传递参数
                Bundle bundle = new Bundle();
                bundle.putSerializable("vouchlist", vouchlist);
                intent.putExtra("listtype", listtype);
                intent.putExtra("currentrow", (int) id);//传递当前行,下一界面便于划动切换
                intent.putExtras(bundle);
                startActivityForResult(intent, R.layout.layout_gcwx_check_detail / 100000);
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
        String djtype = "checklist";
        //对listtype进行转换
        if (listtype.equals("1")) {
            djtype = "alchecklist";
        }
        AbDialogUtil.showProgressDialog(Gcwx_check_index.this, 0, "正在加载...");
        //参数
        String urlString = "http://" + InstorageURL.SERVERURL
                + BaseUrl_New;
        String[] parms = {"usercode", "cacc_id", "iyear", "djtype"};
        String[] vals = {User.uid, myApplication.getGs_ztid(), myApplication.getGs_year(), djtype};
        //生成xml
        BuildGcwxXMLService service = new BuildGcwxXMLService();
        String xmlstring = service.buildXML("gcwx_vouchlist", InstorageURL.NAME_SPACE, parms, vals, null, null, null, null, null, null, null, null);
        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "gcwx_vouchlist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                //恢复当前选中
                adatperDataList.clear();
                ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
                if (result != null) {
                    //解析xml文件,返回列表
                    vouchlist = XMLGcwxVouchlist.paraseCommentInfors(result);
                    for (int i = 0; i < vouchlist.size(); i++) {
                        //增加一个行选择状态初始化
                        sa.getIsSelected().put(i, false);
                        GcwxList wxvouch = new GcwxList();
                        wxvouch = vouchlist.get(i);
                        //                            aaa.add(item.getPropertyAsString(0));
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("index", (i + 1));
                        map.put("cvouchid", "单据号：" + wxvouch.getCvouchid());
                        map.put("cdept", "报修部门：" + wxvouch.getCdepname());
                        map.put("cnocheckreason", "维修区域：" + wxvouch.getCwxplname());
                        map.put("ddate", "报修时间：" + wxvouch.getDdate());
                        //更多数据
                        map.put("data5", "完成时间：" + wxvouch.getDwcdate());
                        map.put("data6", "报修内容：" + wxvouch.getCbxtext());
                        map.put("data7", "");
                        map.put("data8", "");
                        dataListForInit.add(map);
                    }
                } else {
                    AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                }
                toolDataProvider.initData(dataListForInit);
                loadList();
                //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
                //swipeRefreshLayout.setRefreshing(false);

            }

            @Override
            public void onFailure(String result) {
                //swipeRefreshLayout.setRefreshing(false);
                AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                //ToolLog.log("err：" + result);
            }
        });

    }

    //完成后的返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_gcwx_check_detail / 100000:
                getdata(0);
                break;
            case R.layout.basenvo_nofunc / 300000:
                //处理选状态
                linechoose("r", data.getExtras().getInt("currentrow"));
                break;
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
